import React from 'react'
import { Redirect } from 'react-router-dom'

import { checkPermission } from './utils'

export default (key, ...args) => (Component) => {
  return class Auth extends React.Component {
    judgePermission() {
      return checkPermission(key, ...args) ? <Component {...this.props} />
        : <Redirect to={{ pathname: '/error/403', state: { from: this.props.location }}} />
    }
    render() {
      return key ? this.judgePermission() : <Component {...this.props} />
    }
  }
}
